package controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import model.Blog;
import model.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/blog")
//这个Servlet类同于处理获取全部文章(blog_List页面请求)和博客详情页(blog_detail)
//通过判断请求中是否有blogId来分别进行这两种GET请求的逻辑
public class BlogServlet extends HttpServlet {
    //用于构建Json字符串对象的操作对象
    private ObjectMapper objectMapper = new ObjectMapper();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //先告诉Servlet要使用utf8来解析req中的信息
        //req.setCharacterEncoding("text/html;charset=utf8");
        resp.setContentType("application/json;charset=utf8");
        //尝试获取请求中(url)是否包含query String
        // (用于判断请求来自博客列表页or博客详情页)
        String blogId=req.getParameter("blogId");
       //创建一个在数据库中操作博客信息的对象(blogMapper)
        blogMapper blogMapper=new blogMapper();//操作数据库中博客表的对象
        if(blogId!=null){//请求来自博客详情页
            Blog blog=new Blog();
            try {
                blog=blogMapper.selectOne(Integer.parseInt(blogId));
                //System.out.println(blog);
                //将其(查询到的博客信息)构建成Json字符串并生成响应
                String blogJson= objectMapper.writeValueAsString(blog);
                resp.getWriter().write(blogJson);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }else{//请求来自博客列表页
            //该线性表用于存放数据库中查找到的博客列表(多条博客)
            List<Blog> blogList=new ArrayList<>();
            try {
                //获取并将数据填充到线性表中
                blogList=blogMapper.selectAll();
                String listJson= objectMapper.writeValueAsString(blogList);
                resp.getWriter().write(listJson);
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf8");
        HttpSession session=req.getSession();
        if(session==null){
            resp.setStatus(403);
            return;
        }
        User user= (User) session.getAttribute("user");
        if(user==null){
            resp.setStatus(403);
            return;
        }

        String title=req.getParameter("title");
        String content=req.getParameter("content");
        if (title == null || title.equals("") || content == null || content.equals("")) {
            resp.setStatus(400);
            resp.setContentType("text/html; charset=utf8");
            resp.getWriter().write("请求中的标题或正文不完整");
            return;
        }
        blogMapper blogMapper=new blogMapper();
        Blog blog=new Blog();
        blog.setTitle(title);
        blog.setContent(content);
        blog.setUserId(user.getUserId());
        try {
            blogMapper.insert(blog);
            // 4. 插入成功之后, 跳转到博客列表页.
            resp.sendRedirect("blog_List.html");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
